const AdminBiz = require('../../../../../../comm/biz/admin_biz.js');
const pageHelper = require('../../../../../../helper/page_helper.js');
const projectSetting = require('../../../../public/project_setting.js');
const RequestBiz = require('../../../../../../comm/biz/request_biz.js');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isLoad: false,
		searchColumn: 'title'
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		if (!AdminBiz.isAdmin(this)) return;

		wx.setNavigationBarTitle({
			title: projectSetting.ACTIVITY_NAME + '-管理',
		});
		this.setData({
			ACTIVITY_NAME: projectSetting.ACTIVITY_NAME,
			isLoad: true
		});

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () { },

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: async function () {
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () { },

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () { },

	url: async function (e) {
		pageHelper.url(e, this);
	},

	bindCommListCmpt: function (e) {
		pageHelper.commListListener(this, e);
	},

	bindJoinMoreTap: async function (e) {
		if (!AdminBiz.isAdmin(this)) return;

		let activityId = pageHelper.dataset(e, 'id');
		let maxCnt = pageHelper.dataset(e, 'maxcnt');
		let title = encodeURIComponent(pageHelper.dataset(e, 'title'));
		wx.navigateTo({
			url: '../join_list/admin_activity_join_list?activityId=' + activityId + '&title=' + title + '&maxCnt=' + maxCnt,
		});

	},

	bindStatusMoreTap: async function (e) {
		if (!AdminBiz.isAdmin(this)) return;
		let itemList = ['启用', '停用 (不显示)', '删除'];
		wx.showActionSheet({
			itemList,
			success: async res => {
				switch (res.tapIndex) {
					case 0: { //启用
						e.currentTarget.dataset['status'] = 1;
						await this._setStatus(e);
						break;
					}
					case 1: { //停止 
						e.currentTarget.dataset['status'] = 0;
						await this._setStatus(e);
						break;
					}
					case 2: { //删除
						await this._del(e);
						break;
					}
				}
			},
			fail: function (res) { }
		})
	},

	bindMoreTap: async function (e) {
		if (!AdminBiz.isAdmin(this)) return;
		let itemList = ['预览', '生成专属二维码', '查看报名日志'];
		let id = pageHelper.dataset(e, 'id');

		wx.showActionSheet({
			itemList,
			success: async res => {
				switch (res.tapIndex) {
					case 0: { //预览 
						wx.navigateTo({
							url: '../../../activity/detail/activity_detail?id=' + id,
						});
						break;
					}
					case 1: { //二维码 
						let title = encodeURIComponent(pageHelper.dataset(e, 'title'));
						let qr = encodeURIComponent(pageHelper.dataset(e, 'qr'));
						let id = pageHelper.dataset(e, 'id');
						wx.navigateTo({
							url: `../../setup/qr/admin_setup_qr?title=${title}&qr=${qr}&activityId=${id}`,
						})
						break;
					}
					case 2: { //查看操作日志 
						let id = pageHelper.dataset(e, 'id');
						wx.navigateTo({
							url: `../../mgr/log/admin_log_list?&activityId=${id}`,
						})
						break;
					}
				}

			},
			fail: function (res) { }
		})
	},

	_del: async function (e) {
		if (!AdminBiz.isAdmin(this)) return;
		let id = pageHelper.dataset(e, 'id');
    let status = pageHelper.dataset(e, 'status');
    
    if (status === 2) {
      pageHelper.showErrToast('活动已结束');
      return;
    }

		let params = {
			id
		}

		let callback = async () => {
			RequestBiz.request('/api/ysjl/activity/deleteActivity', params).then(res => {
				pageHelper.delListNode(id, this.data.dataList.list, 'id');
				this.data.dataList.total--;
				this.setData({
					dataList: this.data.dataList
				});
				pageHelper.showSuccToast('删除成功');
			});
		}
		pageHelper.showConfirm('确认删除？删除后报名数据将一并删除且不可恢复', callback);

	},

	_setStatus: async function (e) {
		if (!AdminBiz.isAdmin(this)) return;
		let id = pageHelper.dataset(e, 'id');
		let status = pageHelper.dataset(e, 'status');
		let params = {
      id: id,
      status: status
		}
    let req = RequestBiz.request('/api/ysjl/activity/status', params);
		req.then(res => {
			let retData = res.data;
			if (retData.ret == 0) {
        pageHelper.showSuccToast('修改成功');
			}
		});

  },
})